package com.lzh.others.basejava.Thread_21;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Classname ExecutorLear
 * @Description 使用Executors创建线程
 * //TODO 命令设计模式??? 新模式待学习
 * @Date 2021/9/13 17:08
 * @Created by liuzihao
 */
public class CachedThreadPool {

    public static void main(String[] args) throws InterruptedException {
        int countDown = 10;
        int taskCount = 0;
        //
        ExecutorService executorService1 = Executors.newCachedThreadPool();
        ExecutorService executorService2 = Executors.newFixedThreadPool(5);
        ExecutorService executorService3 = Executors.newSingleThreadExecutor();
        List<String> result = new CopyOnWriteArrayList<>();
        CountDownLatch threadSignal = new CountDownLatch(100);
        for (int i = 0; i < 100; i++) {
            int test = i;
            executorService2.execute(() -> {
                result.add(test + "");
                threadSignal.countDown();
            });
        }
        threadSignal.await();
        for (String s : result) {
            System.out.println("s = " + s);
        }
        System.out.println(result.size());
//        for (int i = 0; i < 5; i++) {
//            executorService2.execute(new LiftOff());
//        }
//        executorService2.shutdown();
//        for (int i = 0; i < 5; i++) {
//            executorService3.execute(new LiftOff());
//        }
//        executorService3.shutdown();
    }
}
